using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;


namespace CubeWars___Demolition.GamePlay
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class huds : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        Rectangle powerRectangle;
        Texture2D shootButton, powerBar, crossHair, reset;
        shootingObjects shootingObjects;
        Game1 game;
        bool charging;
        Model sphere;

        public huds(Game1 game)
            : base(game)
        {
            powerRectangle = new Rectangle(48, 446, 0, 20);
            shootingObjects = new shootingObjects(game);
            game.Components.Add(shootingObjects);
            this.game = game;
            // TODO: Construct any child components here
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            sphere = Game.Content.Load<Model>("Models/sphere");
            shootButton = Game.Content.Load<Texture2D>("Textures/shoot");
            powerBar = Game.Content.Load<Texture2D>("Textures/powerBar");
            crossHair = Game.Content.Load<Texture2D>("Textures/crosshair");
            reset = Game.Content.Load<Texture2D>("Textures/reset");
            base.LoadContent();
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            TouchCollection currentTouches = TouchPanel.GetState();
            if (currentTouches.Count == 1 && currentTouches[0].Position.X < 48 && currentTouches[0].Position.Y > 432)
            {
                charging = true;
                shootingObjects.Charging = true;
            }
            if (currentTouches.Count == 0 && charging == true)
            {
                charging = false;
                shootingObjects.Shoot = true;
            }
            powerRectangle.Width = (int)shootingObjects.Power;
            if (currentTouches.Count == 1 && currentTouches[0].Position.X > 700 && currentTouches[0].Position.Y > 432)
            {
                GamePlay.CreateScene.destroyScene(game);
                GamePlay.CreateScene.CreateScene2(game, 5);
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(powerBar, powerRectangle, new Color(new Vector3(255, 255, 255)));
            spriteBatch.Draw(shootButton, new Vector2(0, Game.GraphicsDevice.Viewport.Height - shootButton.Height), new Color(new Vector4(255, 255, 255, 0)));
            spriteBatch.Draw(reset, new Vector2(Game.GraphicsDevice.Viewport.Width - reset.Width, Game.GraphicsDevice.Viewport.Height - reset.Height), new Color(new Vector4(255, 255, 255, 0)));
            spriteBatch.Draw(crossHair, new Vector2((float)Game.GraphicsDevice.Viewport.Width / 2 - (float)crossHair.Width / 2, (float)Game.GraphicsDevice.Viewport.Height / 2 - (float)crossHair.Height / 2), new Color(255, 255, 255));
            spriteBatch.End();
            resetStates();
            base.Draw(gameTime);
        }

        private void resetStates()
        {
            Game.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            Game.GraphicsDevice.BlendState = BlendState.Opaque;
        }
    }
}
